package com.duobaoyu.dby.supply.biz.enums;

import com.duobaoyu.dby.common.core.constant.StringPool;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.util.PropertyPlaceholderHelper;

import java.util.Objects;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName LocalCacheKeys
 * @Description 本地缓存key
 * @createTime 14:53:00 2023/10/07
 */
@Slf4j
@AllArgsConstructor
public enum LocalCacheKeys {

    BIZ_CODE("SUPPLY:BIZCODE:NUMBER:{1}","业务编码缓存");
    private static final PropertyPlaceholderHelper PLACE_HOLDER_HELPER = new PropertyPlaceholderHelper(StringPool.DELIM_START, StringPool.DELIM_END);
    private String cacheKeyPrefix;
    private String desc;

    public String key() {
        return this.cacheKeyPrefix;
    }

    public String desc() {
        return this.desc;
    }

    public String formatKey(final Object... keys) {
        final String template = this.cacheKeyPrefix;
        return PLACE_HOLDER_HELPER.replacePlaceholders(template, (placeholderName) -> {
            if (Objects.equals("1",placeholderName)) {
                if (ArrayUtils.isEmpty(keys)) {
                    log.warn("缓存可以使用错误！,没有设置匹配的占位值【" + template + "】");
                }
                return keys[0].toString();
            }
            if (Objects.equals("2",placeholderName)) {
                if (keys.length < 2) {
                    log.warn("缓存可以使用错误！,没有设置匹配的占位值【" + template + "】");
                }
                return keys[1].toString();
            }
            if (Objects.equals("3",placeholderName)) {
                if (keys.length < 3) {
                    log.warn("缓存可以使用错误！,没有设置匹配的占位值【" + template + "】");
                }
                return keys[2].toString();
            }
            if (Objects.equals("4",placeholderName)) {
                if (keys.length < 4) {
                    log.warn("缓存可以使用错误！,没有设置匹配的占位值【" + template + "】");
                }
                return keys[3].toString();
            }
            return StringPool.EMPTY;
        });
    }
}
